/*
 * UiConfig.cpp
 *
 *  Created on: 2012-10-17
 *      Author: yangentao@gmail.com
 */

#include "UiUtil.h"
#include "UiConfig.h"

int _dic[dicCount] = { 0 };
COLORREF _colors[colorCount] = { 0 };

HBITMAP closeBmp = 0;
HBITMAP closeBmpFocus = 0;


void initDic() {
	_dic[dicTabBarHeight] = 26;
	_dic[dicTabPageMargin] = 3;
	_dic[dicTabRadiusWidth] = 7;
	_dic[dicTabRadiusHeight] = 7;

	_dic[dicTabCloseBmpWidth] = 10;
	_dic[dicTabCloseBmpHeight] = 10;
	_dic[dicTabIconWidth] = 10;
	_dic[dicTabIconHeight] = 13;
	_dic[dicTabTextMargin] = 8;
	_dic[dicTabTextMaxWidth] = 150;
	_dic[dicTabTextMinWidth] = 40;
}
void initColors() {
	_colors[coTabClose] = RGB(93, 101, 114);
	_colors[coTabCloseHot] = RGB(252, 160, 160);
	_colors[coTabCloseMask] = 0;
	_colors[coTabBorder] = RGB(160, 160, 160);
	_colors[coTabBody] = RGB(240, 240, 240);
	_colors[coTabBodySelect] = RGB(220, 220, 220);
	_colors[coTabFocus] = RGB(153, 186, 243);
	_colors[coTabFocusLight] =RGB(229, 237, 252);

}

void initBitmaps();
void initConfig() {
	static bool inited = false;
	if (inited) {
		inited = true;
		return;
	}
	initDic();
	initColors();
	initBitmaps();

}
void cleanConfig() {
	if (closeBmp) {
		DeleteObject(closeBmp);
	}
	if (closeBmpFocus) {
		DeleteObject(closeBmpFocus);
	}
}

HBITMAP getCloseBitmap(bool focus) {
	return focus ? closeBmpFocus : closeBmp;
}


inline int indexArray(int w, int x, int y) {
	return w * y + x;
}

inline void fill4(COLORREF bits[], int w, int h, int x, int y, COLORREF color) {
	bits[indexArray(w, x, y)] = color;
	bits[indexArray(w, w - x - 1, y)] = color;
	bits[indexArray(w, x, h - y - 1)] = color;
	bits[indexArray(w, w - x - 1, h - y - 1)] = color;

}

inline void fillCloseBits(COLORREF bits[], bool focus) {
	COLORREF color = _colors[coTabClose];
	fill4(bits, 10, 10, 0, 0, color);
	fill4(bits, 10, 10, 0, 1, color);
	fill4(bits, 10, 10, 0, 2, color);
	fill4(bits, 10, 10, 1, 0, color);
	fill4(bits, 10, 10, 1, 3, color);
	fill4(bits, 10, 10, 2, 0, color);
	fill4(bits, 10, 10, 2, 4, color);
	fill4(bits, 10, 10, 3, 1, color);
	fill4(bits, 10, 10, 4, 2, color);
	if(focus){
		color = _colors[coTabCloseHot];
		color = reverseColorRB(color);
		fill4(bits, 10, 10, 1, 1, color);
		fill4(bits, 10, 10, 1, 2, color);
		fill4(bits, 10, 10, 2, 1, color);
		fill4(bits, 10, 10, 2, 2, color);
		fill4(bits, 10, 10, 2, 3, color);
		fill4(bits, 10, 10, 3, 1, color);
		fill4(bits, 10, 10, 3, 2, color);
		fill4(bits, 10, 10, 3, 3, color);
		fill4(bits, 10, 10, 3, 4, color);
		fill4(bits, 10, 10, 4, 3, color);
		fill4(bits, 10, 10, 4, 4, color);
	}
}

HBITMAP createCloseBitmap(bool focus) {
	COLORREF bits[100] = { 0};
	fillCloseBits(bits, focus);
	return CreateBitmap(10, 10, 1, 32, bits);
}



void initBitmaps() {
	closeBmp = createCloseBitmap(false);
	closeBmpFocus =createCloseBitmap(true);
}
